package top.lunarye.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.lunarye.handler.TokenToStaffMethodArgumentResolver;

import java.util.List;

/**
 * @description:
 * @author: lunar
 * @since: 2021-07-19
 */
@Configuration
public class MyWebConfiguration implements WebMvcConfigurer {

    @Autowired
    private TokenToStaffMethodArgumentResolver tokenToStaffMethodArgumentResolver;

    @Value("${file.holder}")
    private String FILE_HOLDER;

    /**
     * 配置跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true).maxAge(3600);
    }

    /**
     * 使自定义处理器生效
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(tokenToStaffMethodArgumentResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + FILE_HOLDER + "/image/");
    }
}
